package com.neusoft.bizcore.activiti.service.impl;

import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.activiti.converter.LockDataConverter;
import com.neusoft.bizcore.activiti.dto.LockDataDTO;
import com.neusoft.bizcore.activiti.model.LockData;
import com.neusoft.bizcore.activiti.repository.LockDataRepository;
import com.neusoft.bizcore.activiti.service.LockDataService;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;

@Service
public class LockDataServiceImpl implements LockDataService {

    @Autowired
    private LockDataRepository lockDataRepository;
    @Autowired
    private LockDataConverter lockDataConverter;

    @Override
    public List<LockDataDTO> index(final Searchable searchable) {
        final List<LockData> models = this.lockDataRepository.index(searchable);
        return this.lockDataConverter.toListDTO(models);
    }

    @Override
    public Page<LockDataDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<LockData> result = this.lockDataRepository.search(pageable, searchable);
        final Page<LockDataDTO> pageResult = this.lockDataConverter.toPageDTO(result);
        return pageResult;
    }

    //插入单个(数据加锁)
    @Override
    public LockDataDTO create(final LockDataDTO dto) {
        final LockData model = this.lockDataConverter.toModel(dto);
        this.lockDataRepository.save(model);
        return this.lockDataConverter.toDTO(model);
    }

    //批量插入(数据加锁)
    @Override
    public List<LockDataDTO> createAll(final List<LockDataDTO> dtoList) {
        final List<LockData> model = this.lockDataConverter.toListModel(dtoList);
        this.lockDataRepository.saveAll(model);
        return this.lockDataConverter.toListDTO(model);
    }

    @Override
    public LockDataDTO update(final Long id, final LockDataDTO dto) {
        LockData model;
        try {
            model = this.lockDataRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        dto.setId(model.getId());
        this.lockDataConverter.toModel(dto);
        this.lockDataRepository.save(model);

        return this.lockDataConverter.toDTO(model);
    }

    @Override
    public LockDataDTO details(final Long id) {
        final LockData model;
        try {
            model = this.lockDataRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        final LockDataDTO result = this.lockDataConverter.toDTO(model);
        return result;
    }

    @Override
    public void delete(final Long id) {
        this.lockDataRepository.deleteById(id);
    }

    @Override
    public LockDataDTO findByCode(final String code) {
        final LockData model = this.lockDataRepository.findByCode(code);
        if (model != null) {
            return this.lockDataConverter.toDTO(model);
        }
        return null;
    }

    @Override
    public void deleteByWorkNum(final String workNum) {
        this.lockDataRepository.deleteByWorkNum(workNum);
    }

    @Override
    public void deleteByCode(final String code) {
        this.lockDataRepository.deleteByCode(code);
    }
}
